package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.data.tenant.TenantContextHolder;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicAuthorizationRecordService;
import com.iwomy.secureplat.platform.service.BasicDeviceModelService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.service.BasicVideoModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频类设备型号表
 *
 * @author pig
 * @date 2024-02-18 15:21:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicVideoModel" )
@Tag(description = "basicVideoModel" , name = "视频类设备型号表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicVideoModelController {

    private final  BasicVideoModelService basicVideoModelService;

	private final BasicAuthorizationRecordService basicAuthorizationRecordService;

	private final BasicDeviceService basicDeviceService;

	private final BasicDeviceModelService basicDeviceModelService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicVideoModel 视频类设备型号表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicVideoModel_view')" )
    public R getBasicVideoModelPage(@ParameterObject Page page, @ParameterObject BasicVideoModelEntity basicVideoModel) {
        return R.ok(basicVideoModelService.getBasicVideoModelPage(page, basicVideoModel));
    }


    /**
     * 通过id查询视频类设备型号表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicVideoModel_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicVideoModelService.getById(id));
    }

    /**
     * 新增视频类设备型号表
     * @param basicVideoModel 视频类设备型号表
     * @return R
     */
    @Operation(summary = "新增视频类设备型号表" , description = "新增视频类设备型号表" )
    @SysLog("新增视频类设备型号表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicVideoModel_add')" )
    public R save(@RequestBody BasicVideoModelEntity basicVideoModel) {
		BasicVideoModelEntity wrapperEntity = new BasicVideoModelEntity();
		wrapperEntity.setVideoModelName(basicVideoModel.getVideoModelName());
		wrapperEntity.setProviderId(basicVideoModel.getProviderId());
		LambdaQueryWrapper<BasicVideoModelEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicVideoModelEntity one = basicVideoModelService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			throw new PlatformException("视频类设备型号已存在");
		}
		basicVideoModel.setDeptId(SecurityUtils.getUser().getDeptId());
		basicVideoModel.setTenantId(TenantContextHolder.getTenantId());
		basicVideoModel.setState(10);
        return R.ok(basicVideoModelService.save(basicVideoModel));
    }

    /**
     * 修改视频类设备型号表
     * @param basicVideoModel 视频类设备型号表
     * @return R
     */
    @Operation(summary = "修改视频类设备型号表" , description = "修改视频类设备型号表" )
    @SysLog("修改视频类设备型号表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicVideoModel_edit')" )
    public R updateById(@RequestBody BasicVideoModelEntity basicVideoModel) {
		long count = basicAuthorizationRecordService.count(Wrappers.<BasicAuthorizationRecordEntity>lambdaQuery().eq(BasicAuthorizationRecordEntity::getDeviceModelId, basicVideoModel.getId()));
		long count2 = basicDeviceService.count(Wrappers.<BasicDeviceEntity>lambdaQuery().eq(BasicDeviceEntity::getDeviceModelId, basicVideoModel.getId()));

		if (count > 0 || count2 > 0)
			return R.failed("该视频类设备型号下有关联数据，不能编辑");

		BasicVideoModelEntity wrapperEntity = new BasicVideoModelEntity();
		wrapperEntity.setVideoModelName(basicVideoModel.getVideoModelName());
		wrapperEntity.setProviderId(basicVideoModel.getProviderId());
		LambdaQueryWrapper<BasicVideoModelEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicVideoModelEntity one = basicVideoModelService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(basicVideoModel.getId())) {
			throw new PlatformException("视频类设备型号已存在");
		}
        return R.ok(basicVideoModelService.updateById(basicVideoModel));
    }

    /**
     * 通过id删除视频类设备型号表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频类设备型号表" , description = "通过id删除视频类设备型号表" )
    @SysLog("通过id删除视频类设备型号表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicVideoModel_del')" )
    public R removeById(@RequestBody Long[] ids) {
		long count = basicAuthorizationRecordService.count(Wrappers.<BasicAuthorizationRecordEntity>lambdaQuery().in(BasicAuthorizationRecordEntity::getDeviceModelId, ids));
		long count2 = basicDeviceService.count(Wrappers.<BasicDeviceEntity>lambdaQuery().in(BasicDeviceEntity::getDeviceModelId, ids));

		if (count > 0 || count2 > 0)
			return R.failed("该视频类设备型号下有关联数据，不能删除");
        return R.ok(basicVideoModelService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicVideoModel 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicVideoModel_export')" )
    public List<BasicVideoModelEntity> export(BasicVideoModelEntity basicVideoModel,Long[] ids) {
        return basicVideoModelService.list(Wrappers.lambdaQuery(basicVideoModel).in(ArrayUtil.isNotEmpty(ids), BasicVideoModelEntity::getId, ids));
    }

	/**
	 * 启用禁用
	 *
	 * @param basicVideoModelEntity
	 * @return R
	 */
	@Operation(summary = "启用禁用视频类设备型号", description = "启用禁用视频类设备型号")
	@SysLog(expression = "'启用禁用视频类设备型号'.concat(#basicVideoModelEntity.remarks)")
	@PostMapping("/enableOrDisable")
	public R enableOrDisable(@RequestBody BasicVideoModelEntity basicVideoModelEntity) {
		return R.ok(basicVideoModelService.updateById(basicVideoModelEntity));
	}

	/**
	 * 分页查询
	 * @param basicVideoModelEntity 视频类设备型号表
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/list" )
	public R getBasicDeviceTypeList( @ParameterObject BasicVideoModelEntity basicVideoModelEntity) {
		return R.ok(basicVideoModelService.getBasicVideoModelList(basicVideoModelEntity));
	}
}